package com.shaodongbo.valueanimator;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btn_anim;
    private MyButton btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_anim = findViewById(R.id.btn_anim);
        btn = findViewById(R.id.btn);

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                float currentX = btn.getTranslationX();
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, "translationX", currentX, 500);
                objectAnimator.setDuration(5000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        btn.setTranslationY(value);
                    }
                });
                objectAnimator.start();
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, "scaleX", 1, 5);
                objectAnimator.setDuration(5000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        btn.setScaleY(value);
                    }
                });
                objectAnimator.start();
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, "rotation", 0, 360);
                objectAnimator.setDuration(5000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
//                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, "alpha", 1, 0, 1);
//                objectAnimator.setDuration(5000);
//                objectAnimator.setRepeatCount(3);
//                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
//                objectAnimator.start();
                ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.alpha_anim);
                objectAnimator.setTarget(btn);
                objectAnimator.start();
            }
        });

        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
//                AnimatorSet animatorSet = new AnimatorSet();
//                float currentX = btn.getTranslationX();
//                ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btn, "translationX", currentX, 500);
//                ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(btn, "scaleX", 1, 5);
//                ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(btn, "rotation", 0, 360);
//                ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(btn, "alpha", 1, 0, 1);
//                animatorSet.play(objectAnimator1).with(objectAnimator2).with(objectAnimator3).before(objectAnimator4);
//                animatorSet.setDuration(5000);
//                animatorSet.start();

                AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.anim_set);
                animatorSet.setTarget(btn);
                animatorSet.start();
            }
        });

        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyColor start = new MyColor();
                start.red = 255;
                MyColor end = new MyColor();
                end.green = 255;

                ObjectAnimator objectAnimator = ObjectAnimator.ofObject(btn, "myColor", new MyColorEvaluator(), start, end);

                objectAnimator.setDuration(5000);
                objectAnimator.start();
            }
        });

        btn_anim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPropertyAnimWithJava();
//                startPropertyAnimWithXML();
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Point start = new Point(100, 100);
                Point end = new Point(400, 600);
                ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(),start, end);
                valueAnimator.setDuration(3000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        Point point = (Point) animation.getAnimatedValue();
                        Log.e(TAG, "point x : " + point.getX() + " point y : " + point.getY());
                        btn.setTranslationX(point.getX());
                        btn.setTranslationY(point.getY());
                        btn.requestLayout();
                    }
                });
                valueAnimator.start();
            }
        });
    }

    private void startPropertyAnimWithXML() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_animator);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                btn_anim.getLayoutParams().width = value;
                btn_anim.getLayoutParams().height = value;
                btn_anim.setText("属性动画" + value);
                btn_anim.setBackgroundColor(Color.argb(255, value/2, 0, 0));
                btn_anim.requestLayout();
            }
        });
        valueAnimator.start();
    }

    private void startPropertyAnimWithJava() {
//        final ValueAnimator valueAnimator = ValueAnimator.ofInt(btn_anim.getLayoutParams().width, 300);
//        valueAnimator.setDuration(5000);
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                int value = (int) animation.getAnimatedValue();
//                Log.e(TAG, "value : " + value);
//                btn_anim.getLayoutParams().width = value;
//                btn_anim.requestLayout();
//            }
//        });
        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(btn_anim.getLayoutParams().width, 3);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e(TAG, "value : " + value);
                btn_anim.getLayoutParams().width = (int) value;
                btn_anim.requestLayout();
            }
        });
        valueAnimator.setRepeatCount(5);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.start();
    }
}
